package com.byx.chatclient.view.activity;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.content.ContextCompat;

import com.byx.chatclient.R;
import com.byx.chatclient.bean.SearchUserBean;
import com.byx.chatclient.entity.Constant;
import com.byx.chatclient.entity.Friend;
import com.byx.chatclient.entity.ResultCallback;
import com.byx.chatclient.entity.User;
import com.byx.chatclient.factory.RetrofitFactory;
import com.byx.chatclient.http.ActionRequest;
import com.byx.chatclient.widget.BigImageView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionButton;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionMenu;
import com.oguzdev.circularfloatingactionmenu.library.SubActionButton;

import java.util.Locale;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author Superb
 */
@SuppressLint("NonConstantResourceId")
public class PersonDetailsActivity extends AppCompatActivity {
    @BindView(R.id.iv_back)
    protected ImageView mIvBack;
    @BindView(R.id.iv_menu)
    protected ImageView mIvMenu;
    @BindView(R.id.sdv_avatar)
    protected SimpleDraweeView mSdvAvatar;
    @BindView(R.id.tv_remark_name)
    protected TextView mTvRemarkName;
    @BindView(R.id.tv_nick_name)
    protected TextView mTvNickName;
    @BindView(R.id.iv_sex)
    protected ImageView mIvSex;
    @BindView(R.id.tv_account)
    protected TextView mTvAccount;
    @BindView(R.id.tv_real_name)
    protected TextView mTvRealName;
    @BindView(R.id.tv_phone)
    protected TextView mTvPhone;
    @BindView(R.id.tv_email)
    protected TextView mTvEmail;
    @BindView(R.id.tv_description)
    protected TextView mTvDescription;
    @BindView(R.id.tv_vocation)
    protected TextView mTvVocation;
    @BindView(R.id.rl_person_card)
    protected RelativeLayout mRlCard;
//    @BindView(R.id.tv_send_msg)
//    protected TextView mTvSendMsg;
//    @BindView(R.id.tv_add_friend)
//    protected TextView mTvAddFriend;

    private static final int IS_FRIEND = 200;
    private static final int IS_NOT_FRIEND = 302;
    private static final int IS_NOT_FRIEND_BUT_GROUP = 300;

    private int userIsFriend;

    private Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_details);
        unbinder = ButterKnife.bind(this);
        initCard();
        Intent intent = getIntent();
        userIsFriend = intent.getIntExtra(Constant.INTENT_KEY_IF_FRIEND,0);
        // 判断对方是否为好友
        if (userIsFriend == IS_FRIEND){
            mIvMenu.setVisibility(View.VISIBLE);
            Friend friend = (Friend) intent.getSerializableExtra(Constant.INTENT_KEY_FRIEND);
            initFriend(friend);
        }else {
            mIvMenu.setVisibility(View.GONE);
            User user = (User) intent.getSerializableExtra("user");
            assert user != null;
            RetrofitFactory.getBean(ActionRequest.class).queryUserInfoByLoginId(user.getLoginId())
                    .enqueue(new ResultCallback<User>() {
                        @Override
                        public void onResultSuccess(User data) {
                            init(userIsFriend,data);
                        }

                        @Override
                        public void onResultFailure(int code, String msg) {

                        }
                    });
        }
    }

    private void initCard() {
        int[] images = {R.mipmap.bg_person_detail};
        mRlCard.setBackground(ContextCompat.getDrawable(this, images[0]));
    }

    private void init(int userIsFriend,User user){
        if (user != null) {
            mTvNickName.setText(user.getNickName());
            if (user.getSex() == 0){
                mIvSex.setImageResource(R.mipmap.ic_woman_pink_64);
            }else if (user.getSex() == 1){
                mIvSex.setImageResource(R.mipmap.ic_man_blue_64);
            }
            mTvAccount.setText(String.valueOf(user.getLoginId()));
            mSdvAvatar.setImageURI(user.getAvatar());
            mSdvAvatar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BigImageView.create(PersonDetailsActivity.this).showPhotoViewWithFresco(user.getAvatar());
                }
            });
            String info = String.format(Locale.CHINESE,"%s %d岁 %s",user.getRealName(),user.getAge(),user.getBirthday());
            mTvRealName.setText(info);
            mTvPhone.setText(user.getTelephone());
            mTvEmail.setText(user.getEmail());
            mTvDescription.setText(user.getDescription() != null ? user.getDescription() : "此人很懒，还没有个性签名");
            mTvVocation.setText(user.getVocation());
        }

        // 添加 右下角的白色+号按钮
        final ImageView fabIconNew = new ImageView(this);
        fabIconNew.setImageDrawable(AppCompatResources.getDrawable(this, R.mipmap.ic_add_menu_128px));
        final FloatingActionButton rightLowerButton = new FloatingActionButton.Builder(this)
                .setContentView(fabIconNew)
                .build();
        SubActionButton.Builder rLSubBuilder = new SubActionButton.Builder(this);


        // Build the menu with default options: light theme, 90 degrees, 72dp
        // radius.
        // Set 4 default SubActionButtons
        // FloatingActionMenu通过attachTo(rightLowerButton)附着到FloatingActionButton
        FloatingActionMenu.Builder builder = new FloatingActionMenu.Builder(this);
        FloatingActionMenu rightLowerMenu;
        // TODO: 2021/6/3
        rightLowerButton.setVisibility(View.GONE);

        ImageView rlIcon1 = new ImageView(this);
        ImageView rlIcon2 = new ImageView(this);
        rlIcon1.setImageDrawable(AppCompatResources.getDrawable(this, R.drawable.ic_home_black_24dp));
        rlIcon2.setImageDrawable(AppCompatResources.getDrawable(this, R.drawable.ic_document_box));
        rlIcon1.setVisibility(View.GONE);
        rlIcon2.setVisibility(View.GONE);

        ImageView addFriendIcon = null;
        ImageView sendMsgIcon = null;
        switch (userIsFriend){
            case IS_NOT_FRIEND_BUT_GROUP:
                addFriendIcon = new ImageView(this);
                sendMsgIcon = new ImageView(this);
                addFriendIcon.setImageDrawable(AppCompatResources.getDrawable(this, R.mipmap.ic_add_friend_64));
                sendMsgIcon.setImageDrawable(AppCompatResources.getDrawable(this, R.mipmap.ic_send_message_64_1));
                break;
            case IS_NOT_FRIEND:
                addFriendIcon = new ImageView(this);
                addFriendIcon.setImageDrawable(AppCompatResources.getDrawable(this, R.mipmap.ic_add_friend_64));
                break;
            default:break;
        }
        builder.addSubActionView(rLSubBuilder.setContentView(rlIcon1).build())
                .addSubActionView(rLSubBuilder.setContentView(rlIcon2).build());
        if (addFriendIcon != null){
            builder.addSubActionView(rLSubBuilder.setContentView(addFriendIcon).build());
            addFriendIcon.setOnClickListener(v -> {
                SearchUserBean userBean = new SearchUserBean();
                userBean.setAge(user.getAge());
                userBean.setDescription(user.getDescription());
                userBean.setSex(user.getSex());
                userBean.setAvatar(user.getAvatar());
                userBean.setId(user.getId());
                userBean.setLoginId(user.getLoginId());
                userBean.setNickName(user.getNickName());
                Intent intent = new Intent(PersonDetailsActivity.this, AddFriendActivity.class);
                intent.putExtra(Constant.INTENT_KEY_USER_BEAN,userBean);
                startActivity(intent);
            });
        }
        if (sendMsgIcon != null){
            builder.addSubActionView(rLSubBuilder.setContentView(sendMsgIcon).build());
            sendMsgIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Friend friend = Friend.user2Friend(user,200);
                    Intent it = new Intent(PersonDetailsActivity.this,SingleChatActivity.class);
                    it.putExtra("friend",friend);
                    startActivity(it);
//                    rightLowerButton
                }
            });
        }
        rightLowerMenu = builder.attachTo(rightLowerButton).build();
        // Listen menu open and close events to animate the button content view
        rightLowerMenu.setStateChangeListener(new FloatingActionMenu.MenuStateChangeListener() {
            @Override
            public void onMenuOpened(FloatingActionMenu menu) {
                // 增加按钮中的+号图标顺时针旋转45度
                // Rotate the icon of rightLowerButton 45 degrees clockwise
                fabIconNew.setRotation(0);
                PropertyValuesHolder pvhR = PropertyValuesHolder.ofFloat(View.ROTATION, 135);
                ObjectAnimator animation = ObjectAnimator.ofPropertyValuesHolder(fabIconNew, pvhR);
                animation.start();
            }

            @Override
            public void onMenuClosed(FloatingActionMenu menu) {
                // 增加按钮中的+号图标逆时针旋转45度
                // Rotate the icon of rightLowerButton 45 degrees
                // counter-clockwise
                fabIconNew.setRotation(135);
                PropertyValuesHolder pvhR = PropertyValuesHolder.ofFloat(View.ROTATION, 0);
                ObjectAnimator animation = ObjectAnimator.ofPropertyValuesHolder(fabIconNew, pvhR);
                animation.start();
            }
        });
    }


    private void initFriend(Friend friend){
        mIvMenu.setOnClickListener(v -> {
            Intent intent = new Intent(PersonDetailsActivity.this, FriendPerferenceActivity.class);
            intent.putExtra(Constant.INTENT_KEY_FRIEND,friend);
            startActivity(intent);
            finish();
        });
        if (friend != null) {
            mTvRemarkName.setText(friend.getFriendRemarkName());
            mTvNickName.setText(String.format("(%s)",friend.getFriendNickName()));
            if (friend.getSex() == 0){
                mIvSex.setImageResource(R.mipmap.ic_woman_pink_64);
            }else if (friend.getSex() == 1){
                mIvSex.setImageResource(R.mipmap.ic_man_blue_64);
            }
            mTvAccount.setText(String.valueOf(friend.getFriendLoginId()));
            mSdvAvatar.setImageURI(friend.getFriendAvatar());
            mSdvAvatar.setOnClickListener(v -> {
                BigImageView.create(PersonDetailsActivity.this).showPhotoViewWithFresco(friend.getFriendAvatar());
            });
            String info = String.format(Locale.CHINESE,"%s %d岁 %s",
                    friend.getRealName() == null ? "" :friend.getRealName(),friend.getAge(),
                    friend.getBirthday() == null ? "" : friend.getBirthday());
            mTvRealName.setText(info);
            mTvPhone.setText(friend.getTelephone() == null ? "未知" : friend.getTelephone());
            mTvEmail.setText(friend.getEmail() == null ? "未知" : friend.getEmail());
            mTvDescription.setText(friend.getDescription() != null ? friend.getDescription() : "此人很懒，还没有编辑个人描述");
            mTvVocation.setText(friend.getVocation() == null ? "未知" : friend.getVocation());
        }

        // 添加 右下角的白色+号按钮
        final ImageView fabIconNew = new ImageView(this);
        fabIconNew.setImageDrawable(AppCompatResources.getDrawable(PersonDetailsActivity.this, R.mipmap.ic_add_menu_128px));
        final FloatingActionButton rightLowerButton = new FloatingActionButton.Builder(this)
                .setContentView(fabIconNew)
                .build();
        SubActionButton.Builder rLSubBuilder = new SubActionButton.Builder(this);


        // Build the menu with default options: light theme, 90 degrees, 72dp
        // radius.
        // Set SubActionButtons
        // FloatingActionMenu通过attachTo(rightLowerButton)附着到FloatingActionButton
        FloatingActionMenu.Builder builder = new FloatingActionMenu.Builder(this);
        FloatingActionMenu rightLowerMenu;

        ImageView rlIcon1 = new ImageView(this);
        ImageView rlIcon2 = new ImageView(this);
        rlIcon1.setImageDrawable(AppCompatResources.getDrawable(PersonDetailsActivity.this, R.drawable.ic_home_black_24dp));
        rlIcon2.setImageDrawable(AppCompatResources.getDrawable(PersonDetailsActivity.this, R.drawable.ic_document_box));
        rlIcon1.setVisibility(View.GONE);
        rlIcon2.setVisibility(View.GONE);

        ImageView addFriendIcon = null;
        ImageView sendMsgIcon;
        sendMsgIcon = new ImageView(this);
        sendMsgIcon.setImageDrawable(AppCompatResources.getDrawable(PersonDetailsActivity.this, R.mipmap.ic_send_message_64_1));
//        builder.addSubActionView(rLSubBuilder.setContentView(rlIcon1).build())
//                .addSubActionView(rLSubBuilder.setContentView(rlIcon2).build());
        builder.addSubActionView(rLSubBuilder.setContentView(sendMsgIcon).build());
        sendMsgIcon.setOnClickListener(v -> {
            Intent it = new Intent(PersonDetailsActivity.this,SingleChatActivity.class);
            it.putExtra("friend",friend);
            startActivity(it);
//                    rightLowerButton
        });
        rightLowerMenu = builder.attachTo(rightLowerButton).build();
        // Listen menu open and close events to animate the button content view
        rightLowerMenu.setStateChangeListener(new FloatingActionMenu.MenuStateChangeListener() {
            @Override
            public void onMenuOpened(FloatingActionMenu menu) {
                // 增加按钮中的+号图标顺时针旋转45度
                // Rotate the icon of rightLowerButton 45 degrees clockwise
                fabIconNew.setRotation(0);
                PropertyValuesHolder pvhR = PropertyValuesHolder.ofFloat(View.ROTATION, 135);
                ObjectAnimator animation = ObjectAnimator.ofPropertyValuesHolder(fabIconNew, pvhR);
                animation.start();
            }

            @Override
            public void onMenuClosed(FloatingActionMenu menu) {
                // 增加按钮中的+号图标逆时针旋转45度
                // Rotate the icon of rightLowerButton 45 degrees
                // counter-clockwise
                fabIconNew.setRotation(135);
                PropertyValuesHolder pvhR = PropertyValuesHolder.ofFloat(View.ROTATION, 0);
                ObjectAnimator animation = ObjectAnimator.ofPropertyValuesHolder(fabIconNew, pvhR);
                animation.start();
            }
        });
    }

    @OnClick({R.id.iv_back,R.id.iv_menu})
    protected void onClick(View view){
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_menu:

                break;
            default:break;
        }
    }
    @Override
    protected void onDestroy() {
//        GradientStatusBarUtil.getInstance().unbind();
        unbinder.unbind();
        super.onDestroy();
    }
}